package com.hxd.mall.utils.open

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.TextUtils
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.hxd.mall.R
import com.hxd.mall.constant.HxdGlobalInfo
import java.util.regex.Pattern

object StringUtil {
    /**
     * 字符串着色、单击事件处理
     */
    fun setSpanClick(str: String, spanList: ArrayList<SpanClickBean>): SpannableString {
        return SpannableString(str).apply {
            spanList.forEach {
                setSpan(object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        it.callback()
                    }

                    override fun updateDrawState(ds: TextPaint) {
                        super.updateDrawState(ds)
                        ds.isUnderlineText = false
                    }
                }, it.startIndex, it.endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

                setSpan(
                    ForegroundColorSpan(ContextCompat.getColor(HxdGlobalInfo.instance, it.fontColor)),
                    it.startIndex, it.endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                )
            }
        }
    }

    fun setSpanClick(spanStr: SpannableString, start: Int, end: Int, callback: () -> Unit) {
        spanStr.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                callback()
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.isUnderlineText = false
            }
        }, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spanStr.setSpan(ForegroundColorSpan(Color.parseColor("#e24840")), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
    }


    // 判断一个字符串是否含有数字
    fun hasMumber(content: String): Boolean {
        var flag = false
        val p = Pattern.compile(".*\\d+.*")
        val m = p.matcher(content)
        if (m.matches()) {
            flag = true
        }
        return flag
    }

    /**
     * 过滤掉中文
     * @param str 待过滤中文的字符串
     * @return 过滤掉中文后字符串
     */
    fun filterChinese(str: String): String {
        // 用于返回结果
        var result = str
        val flag = isContainChinese(str)
        if (flag) {// 包含中文
            // 用于拼接过滤中文后的字符
            val sb = StringBuffer()
            // 用于校验是否为中文
            var flag2 = false
            // 用于临时存储单字符
            var chinese: Char = 0.toChar()
            // 5.去除掉文件名中的中文
            // 将字符串转换成char[]
            val charArray = str.toCharArray()
            // 过滤到中文及中文字符
            for (i in charArray.indices) {
                chinese = charArray[i]
                flag2 = isContainChinese(chinese.toString())
                if (!flag2) {// 不是中日韩文字及标点符号
                    sb.append(chinese)
                }
            }
            result = sb.toString()
        }
        return result
    }

    /**
     * 判断字符串中是否包含中文
     * @param str
     * 待校验字符串
     * @return 是否为中文
     * @warn 不能校验是否为中文标点符号
     */
    fun isContainChinese(str: String): Boolean {
        val p = Pattern.compile("[\u4e00-\u9fa5]")
        val m = p.matcher(str)
        return m.find()
    }

    //判断字符串内容是否只有·和汉字
    fun isContainChineseOrPoint(str: String): Boolean {
        val charArray = str.toCharArray()
        // 过滤到中文及中文字符
        for (i in charArray.indices) {
            val chinese = charArray[i]
            //Android中英文和IOS英文是•，IOS中文·
            if (chinese == '·' || chinese == '•' || isContainChinese(chinese.toString())) {
                continue
            } else
                return false
        }
        return true
    }

    class SpanClickBean(
        var name: String = "", var url: String = "",
        var startIndex: Int, var endIndex: Int,
        var fontColor: Int, var callback: () -> Unit = {}
    )

    fun setSize(tv: TextView, str: String) {
        if (TextUtils.isEmpty(str)) return
        val ss1 = SpannableString(str)
//        ss1.setSpan( RelativeSizeSpan(0.7f), 0,1, 0)
        if (str.contains(".")) {
            ss1.setSpan(RelativeSizeSpan(0.7f), str.lastIndexOf("."), str.length, 0)
        }
//        ss1.setSpan( ForegroundColorSpan(Color.RED), 0, 5, 0);// set color
        tv.text = ss1
    }

    fun setIconText(textView: TextView?, imageView: ImageView?, channel: String, title: String) {
        textView?.text = "           ${title}"
        imageView?.setImageResource(
            if ("tmall" == channel) R.mipmap.tm_icon else {
                R.mipmap.tb_icon
            }
        )
    }

}
